/**
 * 
 */
package com.tw.trips.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tw.trips.dao.TravellerDAO;
import com.tw.trips.domain.Activity;
import com.tw.trips.domain.Alert;
import com.tw.trips.domain.Traveller;
import com.tw.trips.domain.Trip;

/**
 * @author M1005264
 * 
 */
@Service
@Transactional
public class TravellerService {

	private static final String DEFAULT_PASSWORD = "default";
	
	@Autowired
	private TravellerDAO travellerDAO;

	/**
	 * @param email
	 * @param password
	 * @return
	 */
	
	//TODO To be tested to ensure returns the correct Traveller with All the Updates and Alerts and Trips - With Activities for the Current Traveller
	public Traveller login(String email, String password) {

		Traveller traveller = travellerDAO.getTravellerByEmail(email);
		if (traveller != null
				&& traveller.getPassword().equalsIgnoreCase(password)) {
			return traveller;
		}

		return null;
	}

	/**
	 * @param newTraveller
	 * @return
	 */
	public Traveller register(Traveller newTraveller) {
		return travellerDAO.save(newTraveller);
	}

	public Set<Trip> addTrip(Traveller curTraveller, Trip newTrip) {
		newTrip.setCreatedBy(curTraveller);
		travellerDAO.saveTrip(newTrip);
		return travellerDAO.getTrips(curTraveller);
	}

	public Set<Traveller> followTraveller(Traveller curTraveller,
			Traveller toFollow) {
		curTraveller.getFriends().add(toFollow);

		Traveller updatedTraveller = travellerDAO.save(curTraveller);
		return updatedTraveller.getFriends();
	}

	//TODO Verify this to return Updated Traveller
	public Traveller addActivityToTrip(Trip trip,
			Activity activity) {
		return travellerDAO.addActivity(trip, activity);
	}

	public Traveller getTraveller(String senderEmail) {
		Traveller traveller = travellerDAO.getTravellerByEmail(senderEmail);
		return traveller;
	}

	public Traveller register(String senderEmail) {
		Traveller newTraveller = new Traveller();
		newTraveller.setEmailID(senderEmail);
		newTraveller.setPassword(DEFAULT_PASSWORD);
		
		Traveller registeredTraveller = travellerDAO.save(newTraveller);

		return registeredTraveller;
	}

	public Traveller addAlert(Alert alert) {
		return travellerDAO.addAlert(alert);
	}

}
